package com.zy.shiro.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.shiro.domain.*;
import com.zy.shiro.mapper.*;
import com.zy.shiro.service.SysUserService;
import com.zy.vo.RoleVo;
import com.zy.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserServiceImpl  extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

  @Autowired
  private SysUserMapper sysUserMapper;
  @Autowired
  private SysRoleMapper sysRoleMapper;
  @Autowired
  private SysUserRoleMapper sysUserRoleMapper;
  @Autowired
  private SysRoleMenuMapper sysRoleMenuMapper;
  @Autowired
  private SysMenuMapper sysMenuMapper;

  @Override
  public SysUser findUserByusername(String username) {
    LambdaQueryWrapper<SysUser> eq = new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName, username);
    SysUser sysUser = sysUserMapper.selectOne(eq);
    return sysUser;
  }

  @Override
  public List<String> queryRoleByUserId(Long userId) {
    List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));
    ArrayList<String> roleList=new ArrayList<>();
    if (sysUserRoles!=null){
      for (SysUserRole sysUserRole : sysUserRoles) {
//        roleList.add(sysUserRole.getRoleId());
        SysRole sysRole = sysRoleMapper.selectOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleId, sysUserRole.getRoleId()));
        roleList.add(sysRole.getRoleName());
      }
    }
    return roleList;
  }

  @Override
  public List<String> queryPermissionByUserId(Long userId) {
    List<String> roleList = this.queryRoleByUserId(userId);
    ArrayList<String> permissions=new ArrayList<>();
    for (String roleId : roleList) {
      SysRoleMenu sysRoleMenu = sysRoleMenuMapper.selectOne(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
      SysMenu sysMenu = sysMenuMapper.selectOne(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getMenuId, sysRoleMenu.getMenuId()));
      permissions.add(sysMenu.getPermisions());
    }
    return permissions;
  }

  @Override
  public SysUser selectUser(String username, String pwd) {
    SysUser sysUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName, username).eq(SysUser::getPassword, pwd));
    return sysUser ;
  }

//  public  IPage<SysRole> queryAllRole(Page<SysRole> page, RoleVo roleVo) {
////    Integer limit = roleVo.getLimit();
////    Integer page = roleVo.getPage();
////    IPage<SysRole> iPage=new IPage<SysRole>();
////    page.addOrder(OrderItem.desc("create_time"));
//    IPage<SysRole> iPage=  sysRoleMapper.selectPage(page,new LambdaQueryWrapper<SysRole>()
//      .like(StringUtils.hasText(roleVo.getRoleName()),SysRole::getRoleName,roleVo.getRoleName())
//      .like(StringUtils.hasText(roleVo.getRemark()),SysRole::getRemark,roleVo.getRemark()));
//    return iPage;
//  }
  @Override
  public IPage<SysUser> queryAllUser(Page<SysUser> page, SysUserVo sysUserVo) {
//    IPage<SysUser>  sysUserIPage = sysUserMapper.selectPage(page, new LambdaQueryWrapper<SysUser>()
//      .like(StringUtils.hasText(sysUserVo.getUserName()),SysUser::getUserName, sysUserVo.getUserName()));
//    return null;
    Page<SysUser> sysUserPage = sysUserMapper.selectPage(page, new LambdaQueryWrapper<SysUser>()
      .like(StringUtils.hasText(sysUserVo.getUserName()),SysUser::getUserName, sysUserVo.getUserName())
      .like(StringUtils.hasText(sysUserVo.getPhoneNum()),SysUser::getPhoneNum, sysUserVo.getPhoneNum()));
   return  sysUserPage;
  }
}
